package com.aipolice.web;

import com.aipolice.pojo.Connews;
import com.aipolice.pojo.Denews;
import com.aipolice.pojo.EditBean;
import com.aipolice.pojo.ResponseInfo;
import com.aipolice.service.ConnewsService;
import com.aipolice.service.DenewsService;
import com.aipolice.utils.ImageUtil;
import com.baidu.ai.aip.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@RestController
public class NewsController {
    @Autowired
    DenewsService denewsService;

    @Autowired
    ConnewsService connewsService;

    @GetMapping("/denews")
    public List<Denews> users(){
        return denewsService.list();
    }

    @GetMapping("getEditNews/{id}")
    public EditBean getEditNews(@PathVariable("id") int id){
        Denews denews = denewsService.find(id);
        Connews connews = connewsService.find(denews.getConid());
        EditBean editBean = new EditBean();
        editBean.setId(denews.getId());
        editBean.setTitle(denews.getTitle());
        editBean.setContent(connews.getContent());
        editBean.setDescribe(denews.getDescribe());
        return  editBean;
    }

    @GetMapping("/phonedenews")
    public ResponseInfo phoneusers(){
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("200");
        List<Denews> denews = denewsService.list();
        responseInfo.setData(GsonUtils.toJson(denews));
        return responseInfo;
    }

    @PostMapping("/news")
    public Denews addDenws(Denews bean,MultipartFile image, String content,HttpServletRequest request) throws IOException {
        System.out.println(content);
        //正文加入数据库
        Connews connews = addConnews(content);
        bean.setConid(connews.getId());
        addDenews(bean,image,request);
        return bean;
    }

    public Denews addDenews(Denews bean,MultipartFile image,HttpServletRequest request) throws IOException {

        Date date = new Date();
        Timestamp nousedate = new Timestamp(date.getTime());
        bean.setDate(nousedate);
        denewsService.add(bean);
        System.out.println(bean.getId());
        saveOrUpdateImageFile(bean,image,request);
        return bean;
    }

    public Connews addConnews(String content){
        Connews connews = new Connews();
        connews.setContent(content);
        Date date = new Date();
        Timestamp nousedate = new Timestamp(date.getTime());
        connews.setDate(nousedate);
        connews.setAuthor("temp");
        connewsService.add(connews);
        return  connews;
    }

    public void saveOrUpdateImageFile(Denews bean, MultipartFile image, HttpServletRequest request)
            throws IOException {
        File imageFolder= new File(request.getServletContext().getRealPath("img/news"));
        File file = new File(imageFolder,bean.getId()+".jpg");
        if(!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        image.transferTo(file);
        BufferedImage img = ImageUtil.change2jpg(file);
        ImageIO.write(img, "jpg", file);
    }


    @DeleteMapping("/denews/{id}")
    public void delete(@PathVariable("id") int id){
        System.out.println("删除方法");
        denewsService.delete(id);
    }




}
